package com.wyl.learn04;

import com.rabbitmq.client.*;
import com.wyl.config.RabbitMqConfig;

import java.nio.charset.StandardCharsets;

/**
 * @author 吴宇亮 on 2023/6/8 15:35
 */
public class Consumer04_1 {

    public static String queueName = "Q1";

    public static void main(String[] args) throws Exception {
        //创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(RabbitMqConfig.IP);
        factory.setUsername(RabbitMqConfig.USERNAME);
        factory.setPassword(RabbitMqConfig.PASSWORD);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, Producer04_1.EXCHANGE_NAME_TOPIC,  "*.orange.*");

        System.out.println(queueName);
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));
        };
        CancelCallback cancelCallback = (String consumerTag) -> {
            System.out.println("消息接收失败：" + consumerTag);
        };
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
    }
}
